﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Rotempco.ORM.Generator.ViewModels;
using Rotempco.ORM.Generator.Objects;
using System.IO;
using Rotempco.ORM.Generator.Managers;

namespace Rotempco.ORM.Generator
{
    /// <summary>
    /// Interaction logic for TableWizard.xaml
    /// </summary>
    public partial class TableWizard : UserControl
    {
        private int _TableIndex;
        private string _TableName;

        private RotempcoSettings _RotempcoSettings;

        public TableWizard()
        {
            InitializeComponent();


        }

        public void SetSettings(RotempcoSettings rotempcoSettings)
        {
            _RotempcoSettings = rotempcoSettings;

            LoadNextTable();
        }

        private void LoadNextTable()
        {
            if (VMTables.Instance.SelectedTables.Count > _TableIndex)
            {
                _TableName = VMTables.Instance.SelectedTables[_TableIndex];
                label_TableName.Content = _TableName;
                VMColumns.Instance.LoadAll(_RotempcoSettings, _TableName);

                string aDalImplementation = _RotempcoSettings.GetValue("@ADALType_" + _TableName);

                ComboBoxItem comboBoxItem = (from c in comboBox_ADALType.Items.Cast<ComboBoxItem>() where c.Content.ToString() == aDalImplementation select c).FirstOrDefault();
                if (comboBoxItem != null)
                    comboBox_ADALType.SelectedItem = comboBoxItem;

                _TableIndex++;

                button_Back.IsEnabled = (_TableIndex > 2);
            }
            else
            {
                MessageBox.Show("Done");

                TableView tableView = new TableView();
                this.Content = tableView;
                tableView.SetSettings(_RotempcoSettings);
            }
        }

        private void dataGrid_ColumtList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
           
        }

        private void button_Next_Click(object sender, RoutedEventArgs e)
        {
            ORMManager.Instance.PerformORM(_RotempcoSettings, new FileInfo(_RotempcoSettings.SettingsFile).Directory.FullName, _TableName, _TableName, (comboBox_ADALType.SelectedItem as ComboBoxItem).Content.ToString(), VMColumns.Instance.Columns.ToList());
            LoadNextTable();
        }

        private void button_Back_Click(object sender, RoutedEventArgs e)
        {
            if (_TableIndex > 1)
            {
                _TableIndex -= 2;

                LoadNextTable();
            }
        }


        private void button_BackToTableSelection_Click(object sender, RoutedEventArgs e)
        {
            TableView tableView = new TableView();
            this.Content = tableView;
            tableView.SetSettings(_RotempcoSettings);
        }


        private void menuItem_SetupEnum_Click(object sender, RoutedEventArgs e)
        {
            DatabaseColumn databaseColumn = (DatabaseColumn)dataGrid_ColumtList.SelectedItem;
            if (databaseColumn == null)
                return;

            EnumEditor enumEditor = new EnumEditor();
            enumEditor.SetSettings(_RotempcoSettings, _TableName, databaseColumn);
            enumEditor.ShowDialog();

            if (enumEditor.EnumName == null)
                return;

            databaseColumn.EnumName = enumEditor.EnumName;
            databaseColumn.EnumNamespace = enumEditor.EnumNamespace;
            databaseColumn.IsEnum = true;

            int index = VMColumns.Instance.Columns.IndexOf(databaseColumn);
            VMColumns.Instance.Columns.Remove(databaseColumn);
            VMColumns.Instance.Columns.Insert(index, databaseColumn);
        }

        private void menuItem_SetupSerializableObject_Click(object sender, RoutedEventArgs e)
        {
            DatabaseColumn databaseColumn = (DatabaseColumn)dataGrid_ColumtList.SelectedItem;
            if (databaseColumn == null)
                return;

            SerializableObjectEditor serializableObjectEditor = new SerializableObjectEditor();
            serializableObjectEditor.SetSettings(_RotempcoSettings, _TableName, databaseColumn);

            if (serializableObjectEditor.SerializableObjectName == null)
                return;

            databaseColumn.SerializableName = serializableObjectEditor.SerializableObjectName;
            databaseColumn.SerializableNamespace = serializableObjectEditor.SerializableObjectNamespace;
            databaseColumn.IsSerializable = true;

            int index = VMColumns.Instance.Columns.IndexOf(databaseColumn);
            VMColumns.Instance.Columns.Remove(databaseColumn);
            VMColumns.Instance.Columns.Insert(index, databaseColumn);
        }


    }
}
